﻿using CCWin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_SaaS_WinForm.物料管理
{
    public partial class MaterialInfoForm : CCSkinMain
    {
        public MaterialInfoForm()
        {
            InitializeComponent();
            this.Load += MaterialInfoForm_Load;
        }

        private async void MaterialInfoForm_Load(object sender, EventArgs e)
        {
            await LoadMaterialsAsync();
        }

        private async Task LoadMaterialsAsync()
        {
            string baseUrl = "http://localhost:5128/api/Material/GetMaterialInfo";
            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage response = await client.GetAsync(baseUrl);
                response.EnsureSuccessStatusCode();
                string jsonResponse = await response.Content.ReadAsStringAsync();
                MaterialApiResponse apiResponse = JsonConvert.DeserializeObject<MaterialApiResponse>(jsonResponse);
                if (apiResponse != null && apiResponse.code == 200)
                {
                    dgvMaterialInfo.AutoGenerateColumns = false;
                    dgvMaterialInfo.DataSource = apiResponse.data ?? new List<MaterialInfoDto>();
                }
                else
                {
                    MessageBox.Show($"查询失败{apiResponse?.msg}");
                }
            }
        }
    }
}
